package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.Carousel;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.bean.Route;
import com.ws.ssmzongjie3.bean.Route_Map;
import com.ws.ssmzongjie3.service.Route_MapService;
import com.ws.ssmzongjie3.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/route_map")
public class Route_MapController {
    @Autowired
    private Route_MapService route_mapService;
    @RequestMapping("/list")
    public String select(Model model, ProVo proVo,Route_Map route_map){
        System.out.println("links:"+route_map);

        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Route_Map> routeMaps = route_mapService.listRoute_Map(route_map);
        PageInfo pageInfo = new PageInfo(routeMaps);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("route_map",route_map);
        return "/WEB-INF/admin/route_map/list";
    }
    @RequestMapping("/add")
    public String add(Route route, Model model) {
        model.addAttribute("route",route);
        return "/WEB-INF/admin/route_map/add";
    }

    @RequestMapping("/insert")
    public String insert(Route_Map route_map, MultipartFile image) throws IOException {

        if ((image.getSize() != 0)||!(image.getOriginalFilename().equals(""))){
            System.out.println("文本框名字"+image.getName());
            System.out.println("文件名字"+image.getOriginalFilename());
            System.out.println("文件大小"+image.getSize());
            System.out.println("文件格式"+image.getContentType());

            String uuid = UUID.randomUUID().toString().replace("-", "");
            System.out.println(uuid);
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            System.out.println(fileName);
            image.transferTo(new File(Res.fileDir+fileName));
            route_map.setRoute_img_url(fileName);
        }

        route_mapService.insertRoute_Map(route_map);
        return "redirect:/route_map/list";
    }

    @RequestMapping("/delete")
    public String delete(Route_Map route_map) {
        Route_Map routeMapById = route_mapService.getRoute_MapById(route_map);
        if (routeMapById != null && routeMapById.getRoute_img_url() != null) {
            File imageFile = new File(Res.fileDir + routeMapById.getRoute_img_url());
            if (imageFile.exists()) {
                imageFile.delete();
            }
        }

        route_mapService.deleteRoute_Map(route_map);
        return "redirect:/route_map/list";
    }

    @RequestMapping("/update")
    public String update(Route_Map route_map,MultipartFile image) throws IOException {


        Route_Map routeMapById = route_mapService.getRoute_MapById(route_map);
        if (routeMapById != null && routeMapById.getRoute_img_url() != null) {
            File imageFile = new File(Res.fileDir + routeMapById.getRoute_img_url());
            if (imageFile.exists()) {
                imageFile.delete();
            }
        }

        if ((image.getSize() != 0)||!(image.getOriginalFilename().equals(""))){
            System.out.println("文本框名字"+image.getName());
            System.out.println("文件名字"+image.getOriginalFilename());
            System.out.println("文件大小"+image.getSize());
            System.out.println("文件格式"+image.getContentType());

            String uuid = UUID.randomUUID().toString().replace("-", "");
            System.out.println(uuid);
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            System.out.println(fileName);
            image.transferTo(new File(Res.fileDir+fileName));
            route_map.setRoute_img_url(fileName);
        }

        route_mapService.updateRoute_Map(route_map);
        return "redirect:/route_map/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Route_Map route_map, Model model) {
        Route_Map route_map1 = route_mapService.getRoute_MapById(route_map);
        model.addAttribute("route_map", route_map1);
        return "/WEB-INF/admin/route_map/update";
    }


}